#!/usr/bin/env bash

APP_NAME=authrization-manager
APP_HOME="${BASH_SOURCE-$0}"
APP_HOME="$(dirname "${APP_HOME}")"
APP_HOME="$(cd "${APP_HOME}"; pwd)"
PID_FILE="$APP_HOME/../pid/app.pid"

echo "Stopping $APP_NAME ... "
if [ ! -f "$PID_FILE" ]
then
  echo "no $APP_NAME to stop (could not find file $PID_FILE)"
else
  PID=$(cat "$PID_FILE")
  kill $PID
  COUNT=0
  while [ $COUNT -lt 1 ]; do
    echo -e ".\c"
    sleep 1
    COUNT=1
    PID_EXIST=`ps -f -p $PID | grep java`
    if [ -n "$PID_EXIST" ]; then
        COUNT=0
    fi
  done
  rm "$PID_FILE"
  echo "$APP_NAME stopped"
fi
exit 0
